function Block(container) {
    this.container = container;
    this.mainW = this.container.parentNode.clientWidth; //获取父元素宽度
    this.mainH = this.container.parentNode.clientHeight; //获取父元素高度
    this.scale = 1.58; //黑块的高度比
    this.height = parseInt(this.mainW / 4 * this.scale); //定义黑块高度
    this.top = -this.height;
    this.speed = 2; //定义速度
    this.maxSpeed = 20; //定义最大速度
    this.timer = null; //定时器id
    this.state = true; //游戏状态
    this.sum = 0; //游戏分数
}
Block.prototype = {
    init: function () {
        var _t = this;
        _t.mark(); // 显示初始分解
        _t.container.addEventListener("click", function (e) {
            if (!_t.state) {
                return false;
            }
            e = e || window.event; //获取事件对象
            var target = e.target || e.srcElement; //获取触发事件的元素
            if (target.className.indexOf('block') != -1) {
                _t.sum++;
                document.getElementsByClassName("mark")[0].innerHTML = _t.sum;
                target.className = 'blank'; //设置类名
            } else {
                _t.state = false;
                clearInterval(_t.timer);
                _t.end();
                return false;
            }
        });
    },
    //显示分数
    mark: function () {
        var oMark = document.createElement("div"); //创建div元素
        oMark.className = "mark";
        oMark.innerHTML = this.sum;
        this.container.parentNode.appendChild(oMark);
    },
    //创建一行
    addRow: function () {
        var oRow = document.createElement("div"); //创建div元素
        oRow.className = 'row';
        oRow.style.height = this.height + 'px';
        var blanks = ['blank', 'blank', 'blank', 'blank'];
        var s = Math.floor(Math.random() * 4); //获取0-3随机数
        blanks[s] = "blank block";
        var oBlank = null;
        for (var i = 0; i < 4; i++) {
            oBlank = document.createElement("div");
            oBlank.className = blanks[i]; //设置类名
            oRow.appendChild(oBlank); //添加元素
        }

        var fChild = this.container.fristChild; //获取第一个元素
        if (fChild == null) {
            this.container.appendChild(oRow); //在最后面添加元素
        } else {
            this.container.insertBefore(oRow, fChild); //在最前面添加元素
        }
    },

    //游戏界面向下移动
    move: function () {
        this.top += this.speed;  //设置top值
        this.container.style.top = this.top + 'px'; //设置元素位置
    },

    //判断游戏状态
    judge: function () {
        var t = this;
        if (t.top >= 0) {
            t.top = -this.height; //设置top值
            t.container.style.top = t.top + 'px'; //设置元素位置
            t.addRow();
        }
        t.speed = (parseInt(t.sum / 5) + 1) * 2; //根据单机的黑块儿总数提高速度
        if (t.speed >= t.maxSpeed) {
            t.speed = t.maxSpeed;
        }
        var blocks = docunment.getElementsByClassName('block'); //获取黑块儿
        for (var j = 0; j < blocks.length; j++) {
            if (blocks[j].offsetTop >= t.mainH) {
                t.state = false;
                clearInterval(t.timer);
                t.end();
            }
        }
    },

    //开始游戏
    start: function () {
        var t = this;
        for (var i = 0; i < 4; i++) {
            t.addRow();
        }
        t.timer = setInterval(function () {
            t.move();
            t.judge();
        }, 30);
    },

    //游戏结束界面
    end: function () {
        var t = this;
        if (!document.getElementById("result")) {
            var result = document.createElement("div");
            result.className = 'result';
            result.id = 'result';
            result.innerHTML = '<h1>GAME OVER</h1><h2 id="score">分数：' + t.sum + '</h2><span id="restart">重新开始</span>';
        } else {
            var result = document.getElementById("result");
            result.style.display = "block";
            var score = document.getElementById("score");
            score.innerHTML = '分数: ' + t.sum;
        }

        var restart = document.getElementById("restart");
        restart.onclick = function () {
            t.again();
            result.style.dispaly = "none";
            return false;
        }
    },

    //重新开始游戏
    again: function () {
        this.mainW = this.container.parentNode.clientWidth;
        this.mainH = this.container.parentNode.clientHeight;
        this.scale = 1.58; //黑块的高度比
        this.height = parseInt(this.mainW / 4 * this.scale); //定义黑块高度
        this.top = -this.height;
        this.speed = 2; //定义速度
        this.maxSpeed = 20; //定义最大速度
        this.timer = null; //定时器id
        this.state = true; //游戏状态
        this.sum = 0; //游戏分数
        var t = this;
        t.container.innerHTML = "";
        document.getElementsByClassName('mark')[0].innerHTML = t.sum;
        t.start();
    }
}